package cn.jane.rpc.test.client;

import cn.jane.rpc.api.NameService;
import cn.jane.rpc.api.RpcAccessPoint;
import cn.jane.rpc.api.spi.ServiceSupport;
import cn.jane.rpc.test.rpc.TestRpc;
import cn.jane.rpc.test.util.NameServiceUtil;
import java.io.IOException;
import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用于测试的客户端
 */
public class Client {

    private static final Logger log = LoggerFactory.getLogger(Client.class);

    public static void main(String[] args) throws IOException {
        String param = "jane";
        String serviceName = TestRpc.class.getName();
        URI nameServiceURI = NameServiceUtil.getNameServiceURI();

        try (RpcAccessPoint rpcAccessPoint = ServiceSupport.load(RpcAccessPoint.class)) {
            NameService nameService = rpcAccessPoint.getNameService(nameServiceURI);
            assert nameService != null;
            URI uri = nameService.lookupService(serviceName).get(0);
            assert uri != null;

            log.info("find NameService: {}, server: {}", serviceName, uri);
            TestRpc testRpc = rpcAccessPoint.getRemoteService(uri, TestRpc.class);

            log.info("call \"sysHello\" method, param is {}", param);
            String response = testRpc.sayHello(param);

            log.info("receive response: {}", response);
        }
    }
}
